package strutil

import (
	"regexp"
	"strings"
)

func Sub(str string, from int, to int) string {
	array := []rune(str)
	return string(array[from:to])
}

func SubFrom(str string, from int) string {
	array := []rune(str)
	return string(array[from:])
}

func SubTo(str string, to int) string {
	array := []rune(str)
	return string(array[0:to])
}

func StringCut(str string, from string, to string) string {
	i := strings.Index(str, from)
	if i == -1 {
		return ""
	}

	str = str[i+len(from):]
	j := strings.Index(str, to)
	if j == -1 {
		return ""
	}
	return str[:j]
}

type CaseInsensitiveReplacer struct {
	toReplace   *regexp.Regexp
	replaceWith string
}

func NewCaseInsensitiveReplacer(toReplace, replaceWith string) *CaseInsensitiveReplacer {
	return &CaseInsensitiveReplacer{
		toReplace:   regexp.MustCompile("(?i)" + toReplace),
		replaceWith: replaceWith,
	}
}

func (cir *CaseInsensitiveReplacer) Replace(str string) string {
	return cir.toReplace.ReplaceAllString(str, cir.replaceWith)
}
